package one.stand.service.impl;

import one.stand.mapper.ThumbsMapperExt;
import one.stand.model.Thumbs;
import one.stand.service.ThumbsService;
import one.stand.service.user.UserAdminService;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ThumbsServiceImpl implements ThumbsService {

    @Autowired
    private UserAdminService userAdminService;

    @Autowired
    private ThumbsMapperExt thumbsMapperExt;

    @Override
    public void save(Integer userId, Integer itemType, Integer itemId) {
        Integer id = thumbsMapperExt.select(itemType, itemId, userId);
        if (Objects.nonNull(id)) {
            thumbsMapperExt.deleteByPrimaryKey(id);
        } else {
            String name = userAdminService.get(userId).getName();

            Thumbs model = new Thumbs();
            model.setUserId(userId);
            model.setItemType(itemType);
            model.setItemId(itemId);
            model.setUserName(name);
            model.setCreateTime(DateUtil.currentDate());
            thumbsMapperExt.insertSelective(model);
        }
    }
}
